﻿
namespace MyFurion.Application
{
    /// <summary>
    /// 用户信息服务仓储
    /// </summary>
    public class UserRepository:BaseRepository<UserInfo>, ITransient
    {
        /// <summary>
        /// 查询条件
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private Expression<Func<UserInfo, bool>> GetCodition(UserInfoPage input)
        {
            var exp = Expressionable.Create<UserInfo>();
            exp.And(it => it.IsSuperAdmin == false);
            exp.AndIF(!string.IsNullOrWhiteSpace(input.OrgName),it=>it.OrgData.OrgName.Contains(input.OrgName));
            exp.AndIF(!string.IsNullOrWhiteSpace(input.UserName), it => it.UserName.Contains(input.UserName));
            exp.AndIF(!string.IsNullOrWhiteSpace(input.NickName), it => it.NickName.Contains(input.NickName));
            return exp.ToExpression();
        }
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<UserInfo>>GetPageList(UserInfoPage input)
        {
            var whereExp = GetCodition(input);
            RefAsync<int> totalCount = 0;
            var result = await Context.Queryable<UserInfo>()
                .Includes(it=>it.OrgData)
                .Where(whereExp)
                .OrderByDescending(it=>it.CreateTime)
                .ToPageListAsync(input.PageIndex,input.PageSize, totalCount);
            foreach(var item in result)
            {
                item.LoginPwd = "";
            }
            var pageResult = new PageResult<UserInfo>();
            pageResult.Items = result;
            pageResult.TotalCount = totalCount;
            pageResult.TotalPage = (int)Math.Ceiling(totalCount / (double)input.PageSize);
            return pageResult;
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<List<UserInfo>> GetDataList(UserInfoPage input)
        {
            var whereExp = GetCodition(input);
            return await Context.Queryable<UserInfo>()
                .Includes(it => it.OrgData)
                .Where(whereExp)
                .OrderByDescending(it => it.CreateTime)
                .ToListAsync();
        }
        /// <summary>
        /// 根据账号密码获取人员信息
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public async Task<UserInfo> GetByAccount(string account,string pwd)
        {
            return await Context.Queryable<UserInfo>().Includes(it => it.OrgData).Where(it =>it.Account==account&&it.LoginPwd==pwd).FirstAsync();
        }
        /// <summary>
        /// 根据id获取人员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<UserInfo>GetDetail(long id)
        {
            return await Context.Queryable<UserInfo>().Includes(it => it.OrgData).Where(it => it.Id == id).FirstAsync();
        }
        /// <summary>
        /// 是否空表
        /// </summary>
        /// <returns></returns>
        public async Task<bool> IsEmptyData()
        {
            int count = await Context.Queryable<UserInfo>().Filter(null, true).CountAsync();
            return count == 0;
        }
        /// <summary>
        /// 数据是否存在
        /// </summary>
        /// <param name="account"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DataExists(string account, long? id = null)
        {
            return await Context.Queryable<UserInfo>()
                .Where(it => it.Account == account)
                .WhereIF(id.HasValue, it => it.Id != id.Value)
                .AnyAsync();
        }
    }
}
